# coding:utf-8
import xml
from xml.etree.ElementTree import ElementTree,Element  
import os
import shutil
from multiprocessing import Process
nakes_prefix = r"F:\boxDetect\picture"
images_prefix = r"F:\boxDetect\cshi"

base_dir = "D:\photo_train\object_train_photo\source_photo\\blackApple\\"
replace_dir = "/data/station/object_train_voc/JPEGImages/"


def read_xml(xml_path):
    tree = ElementTree()
    tree.parse(xml_path)
    # path = tree.findall('path')[0]
    # folder = tree.findall('folder')[0]
    filen = tree.findall('object/name')[0]
    e_text = filen.text
    if e_text != "apple":
        e_text = "apple"
    filen.text = e_text
    # p_text = path.text.replace("\\",'/')
    # path.text = p_text
    print(filen.text)
    # obj = tree.findall('object/name')[0]
    # obj.text = needname
    # print(obj.text)
    # namend = obj.findall('name')[0]
    # print(namend.text)
    # weiba = needname+".jpg"
    # p_text = path.text
    # e_text = filen.text
    # p_text = p_text.replace(base_dir,replace_dir)
    # prefix,suffix = os.path.split(p_text)

    # print(p_text)
    # ptext = os.path.join(prefix,weiba)
    # folder.text = 'JPEGImages'
    # filen.text = weiba
    # path.text = ptext
    # # print(p_text)
    # print(filen.text)
    # print(folder.text)
    # print(path.text)
    # path.text = ftext
    # print(f_text)
    # print(all_nodes.tag,dir(all_nodes))
    # node = all_nodes
    # text = node.text
    # # text = text.split("save\\")[-1]
    # text = os.path.split(text)
    # h,t = text[0],text[-1]
    # ftext = f_text +'_'+t
    # print(h,ftext)
    # npath = os.path.join(h,ftext)
    # print(npath)
    # node.text = npath
    # print(e_text)
    tree.write(xml_path, encoding="utf-8")
    return tree

# def find_nodes(tree,path):
#     return tree.findall(path)

# import xml.dom.minidom
# from xml.dom.minidom import parse
# from xml.dom.minidom import Document
# from xml.etree import ElementTree

# def read_cfirstChild.data = 'div1_changed_text'

def change_name(nake_dir,nake_xml):
    xmlp = os.path.join(nake_dir,nake_xml)
    n_dir = os.path.split(nake_dir)[-1]
    npaht = n_dir + "_" + nake_xml
    xmld = os.path.join(nake_dir,npaht)
    os.rename(xmlp,xmld)
    
def do(nake_dir):
    for nake_xml in os.listdir(nake_dir):
        nake_xml = os.path.join(nake_dir,nake_xml)
        try:
            read_xml(nake_xml,'path')
            # change_name(nake_dir,nake_xml)
        except:
            print(nake_xml)
import re
def dcopy():
    targetdir = r"E:\src\keras-yolo3-master\VOCdevkit\VOC2007\JPEGImages"
    xmldir = r"F:\boxDetect\picture\eepics"
    pigdir = r'F:\boxDetect\cshi\A'
    x = []
    for xmlp in os.listdir(xmldir):
        pass
        # xmls = os.path.join(xmldir,xmlp)
        # print(xmls)
        # xmlt = xmlp.replace('eepics','ee')
        # xmlf = os.path.join(xmldir,xmlt)
        # print(xmlf)
        # os.rename(xmls,xmlf)
        # pattern = re.compile('\w(\d+)')
        # dig = re.findall(pattern,xmlp)[0]
        # x.append(dig)
    # for imgp in os.listdir(pigdir):
        # pattern = re.compile('\w(\d+)')
        # dig = re.findall(pattern,imgp)[0]
        # if dig in x:
        #     img = os.path.join(pigdir,imgp)
        #     shutil.copy(img,targetdir)
        #     print(img)
        #     print(dig)
if __name__ == "__main__":
    # dcopy()
    # read()
    # d = r'F:\boxDetect\cshi\A'
    # n = '0.jpg'
    # change_name(d,n)
    # td = r'/home/hiicy/redldw/ldw/models-master/research/voc/VOCdevkit/VOC2007/Annotations'
    td = r'F:\Archive\apple_A'
    for xmlp in os.listdir(td):
        xmlpp = os.path.join(td,xmlp)
        # xmlp = xmlp.split(".")[0]
        read_xml(xmlpp)